package com.hongyuan.venue.utils;


import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.venue.R;
import com.hongyuan.venue.data.md5.BaseUtil;
import com.hongyuan.venue.myview.scllor_view.UnitBean;
import com.hongyuan.venue.myview.scllor_view.UnitPicker;

import java.util.List;

public class CustomDialog {
    /*
     * 点击事件的回调 接口
     * */
    public interface DialogClick{
        void dialogClick(View v);
    }

    /*
     * 用于修改的接口回调
     * */
    public interface DialogClickMessage{
        void dialogClick(View v, String message);
    }

    /*
     * 用于修改的接口回调
     * */
    public interface DialogClickTwoMessage{
        void dialogClick(View v, String message1, String message2);
    }

    /*
     * 用于修改的接口回调
     * */
    public interface DialogClickMessageAndId{
        void dialogClick(View v, String message, String id);
    }

    /*
     * 拨打电话弹框
     * */
    public static void callTel(Context mContext, String telNum , DialogClick dialogClick ){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_call_tel,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView telContent= view.findViewById(R.id.telContent);
        telContent.setText(telNum);
        view.findViewById(R.id.cancel).setOnClickListener(v -> dialog.dismiss());
        view.findViewById(R.id.call).setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
    }

    /*
     * 查看H5 url连接
     * */
    public static void showUrl(Context mContext, String url){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_show_url,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView tv = view.findViewById(R.id.urlTv);
        tv.setText(url);
    }


    /*
     * 1.普通对话弹框
     * 2.有两个取消和确定两个按钮，并按钮有回调。
     * */
    public static void promptDialog(Context mContext,String messagesText,String isOkText,String isCannelText,boolean outSide,DialogClick dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_have_yes_no_prompt,null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(outSide);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView messages = view.findViewById(R.id.messages);
        TextView isOk = view.findViewById(R.id.isOk);
        TextView isCannel = view.findViewById(R.id.isCannel);

        isOk.setText(isOkText);
        isCannel.setText(isCannelText);
        messages.setText(messagesText);

        isOk.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
        isCannel.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
    }

    /*
     * 1.普通对话弹框
     * 2.单个按钮
     * */
    public static void promptDialog(Context mContext,String messagesText,String isOkText,boolean outSide,DialogClick dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_have_yes_no_prompt,null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(outSide);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView messages = view.findViewById(R.id.messages);
        TextView isOk = view.findViewById(R.id.isOk);
        TextView isCannel = view.findViewById(R.id.isCannel);
        View line = view.findViewById(R.id.line);
        line.setVisibility(View.GONE);
        isCannel.setVisibility(View.GONE);

        isOk.setText(isOkText);
        messages.setText(messagesText);

        isOk.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
        isCannel.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
    }


    /*
     * 滚动选择控件
     * */
    public static void scroller(Context mContext, List<UnitBean> mList, String titleText , DialogClickMessageAndId dialogClick ){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_scroller,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView titleView= view.findViewById(R.id.titleName);
        UnitPicker content = view.findViewById(R.id.content);
        content.setData(mList);
        titleView.setText(titleText);
        view.findViewById(R.id.closeImg).setOnClickListener(v -> dialog.dismiss());
        view.findViewById(R.id.submit).setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v,content.getCurrentUnit().unit_cn,content.getCurrentUnit().unit);
            }
            dialog.dismiss();
        });
    }

    /*
     * 编辑资料---修改昵称
     * */
    public static void updateName(Context mContext,DialogClickMessage dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_update_user_name,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        EditText content = view.findViewById(R.id.content);

        view.findViewById(R.id.userNameSubmit).setOnClickListener(v -> {
            if(!BaseUtil.isValue(content.getText().toString())){
                ToastUtils.showShort("请输入!");
            }else{
                dialog.dismiss();
                dialogClick.dialogClick(v,content.getText().toString());
            }

        });
    }

    /*
     * 编辑资料---修改性别
     * */
    public static void updateSex(Context mContext,int sex,DialogClickMessage dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_person_update_sex,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView clickMan = view.findViewById(R.id.clickMan);
        TextView clickWoman = view.findViewById(R.id.clickWoman);


        if(sex == MAN_CLICK){
            click_num = MAN_CLICK;
            changeSex(mContext,clickMan,clickWoman,click_num);
        }else{
            click_num = WOMAN_CLICK;
            changeSex(mContext,clickMan,clickWoman,click_num);
        }

        clickMan.setOnClickListener(v -> {
            if(click_num != MAN_CLICK){
                click_num = MAN_CLICK;
                changeSex(mContext,clickMan,clickWoman,click_num);
            }
        });
        clickWoman.setOnClickListener(v -> {
            if(click_num != WOMAN_CLICK){
                click_num = WOMAN_CLICK;
                changeSex(mContext,clickMan,clickWoman,click_num);
            }
        });

        view.findViewById(R.id.userSexSubmit).setOnClickListener(v -> {
            if(sex != click_num){
                dialog.dismiss();
                dialogClick.dialogClick(v,String.valueOf(click_num));
            }else{
                ToastUtils.showShort("请选择性别!");
            }

        });
    }

    //标识男
    public static final int MAN_CLICK = 1;
    //标识nv
    public static final int WOMAN_CLICK = 2;
    //标识当前点击值
    private static int click_num = MAN_CLICK;

    /*
     * 改变性别选择效果状态
     * */
    private static void changeSex(Context mActivity,TextView clickMan,TextView clickWoman,int click_num){

        Animation amplification = AnimationUtils.loadAnimation(mActivity,R.anim.scale_amplification);
        Animation narrow = AnimationUtils.loadAnimation(mActivity,R.anim.scale_narrow);
        if(click_num == MAN_CLICK){
            clickMan.startAnimation(amplification);
            clickWoman.startAnimation(narrow);
            clickMan.setTextColor(0xFFEF5B48);
            clickWoman.setTextColor(0xFF999999);
        }
        if(click_num == WOMAN_CLICK){
            clickMan.startAnimation(narrow);
            clickWoman.startAnimation(amplification);
            clickMan.setTextColor(0xFF999999);
            clickWoman.setTextColor(0xFFEF5B48);
        }
    }

    /*
     * 编辑资料---修改个性签名
     * */
    public static void updateSign(Context mContext,DialogClickMessage dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_update_user_sign,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        EditText content = view.findViewById(R.id.content);

        view.findViewById(R.id.userSignSubmit).setOnClickListener(v -> {
            if(!BaseUtil.isValue(content.getText().toString())){
                ToastUtils.showShort("请输入!");
            }else{
                dialog.dismiss();
                dialogClick.dialogClick(v,content.getText().toString());
            }

        });
    }


}
